var contactList = { 
	basicURL: location.hostname,
	contactId: "",
    paramsMap: {}, 
        
    loadContactList: function () {    	
    	$.getJSON('getContactList.action', function(ret) {
    		resp = ret;            	
        	contactList.buildContactList(resp.contactList);
    	});    	
    },
    
    buildContactList: function(list) {    	
    	contactList.initContactList(list);
    	contactList.refreshContentList();    	
    },
    
    /**
     * <li data-role="list-divider">A</li>
     * <li><a href="#detail">Adam Kinkaid</a></li>
     */
    initContactList: function(list){    	    	    	
    	$.each(list, function(index, value) {
    		var alias = value.alias;
			var id = value.id;			
			var caption = $("#" + value.category);
			caption.show();
			$("#" + id).remove();
			caption.after($('<li id='+id+'><a href="pages/contactDetail.jsp?id=' + id + '">' + alias+ '</a></li>'));		
			
			/*
			 * caption.after($('<li><a href="#contactDetail?id=' + id + '">' + alias+ '</a></li>'));
			caption.after($("<li id='"+id+"'><a href='#contactDetail'>" + alias+ "</a></li>"));
    		$('#'+id).bind('click', function (event) {    			    			
    			contactDetail.loadContactDetail(id);	
    		});*/
    		
    	});    	    	    									
    },
       
        
    refreshContentList: function() {
    	var listView = $("#contactlistView"); 
    	listView.listview('refresh', true); 
    }
        
   
};

var contactDetail = { 				
		
		loadContactDetail: function(id) {				
			$.getJSON('getContactBasicDetail!loadContact', { "id" : id }, function(ret) {
	    		resp = ret;	    		
	    		contactDetail.buildContactDetail(resp.contact);
	    	});	    			   
	    },
	    
	    buildContactDetail: function(contact) {			
			contactDetail.initContactDetail(contact);			
	    },
	    
	    initContactDetail: function(contact) {		    	
	    	$("#aliasTextId").html("").append(contact.alias);
	    	$("#titleTextId").html("").append(contact.title);
	    	$("#departmentTextId").html("").append(contact.department);	    	
	    	$("#mobileTextId").html("").append(contact.mobile);	    	
	    	$("#emailDetailTextId").html("").append(contact.email);
	    	$("#phoneTextId").html("").append(contact.phone);
	    	$("#faxTextId").html("").append(contact.fax);
	    	$("#officeTextId").html("").append(contact.office);	    	
	    	//Icon click binding
	    	$("#phoneIconId").attr("href", "tel:"+contact.mobile);
	    	$("#chatIconId").attr("href", "sms:"+contact.mobile);
	    	$("#emailIconId").attr("href", "mailto:" + contact.email);	  	    		    	
	    }
		
};


var cookie = {		      

       setCookie: function (cookieName, cookieValue) {
           var today = new Date();
           var expire = new Date();          
           expire.setTime(today.getTime() + 3600000 * 24 * 365);
           document.cookie = cookieName + "=" + escape(cookieValue)+ ";expires=" + expire.toGMTString();

       },

       getCookie: function (cookieName) {
           var theCookie = " " + document.cookie;
           var ind = theCookie.indexOf(" " + cookieName + "=");
           if (ind == -1) ind = theCookie.indexOf(";" + cookieName + "=");
           if (ind == -1 || cookieName == "") return "";
           var ind1 = theCookie.indexOf(";", ind + 1);
           if (ind1 == -1) ind1 = theCookie.length;
           return unescape(theCookie.substring(ind + cookieName.length + 2, ind1));
       }

};


var verify = {
		email: "",
		IMEI: "",
		
		elementBinding: function() {			
			
			$("#emailTextId").click(function() {
				$("#emailTextId").val("");  
			});
			
			$("#netCodeTextId").click(function() {
				$("#netCodeTextId").val("");  
			});						
			
			$("#netCodeBtnId").bind('click', function (event) {
				verify.email = $("#emailTextId").val();
				var option = { 
						data: {"email" : verify.email}, 
						url: 'verifyNetCode!generateNetCode',
		                success: function (ret) {            	
		                	resp = ret; 		                	

		                }    
		            };
		           
		            $.ajax(option);	    		
	    	}); 
			
			$("#verifyBtnId").bind('click', function (event) {  
				var option = { 
						data: {"email":verify.email, "netCode" : $("#netCodeTextId").val()}, 
						url: 'verifyNetCode',
		                success: function (ret) {            	
		                	resp = ret;		                	
		                	if (resp.valid) {
		                		cookie.setCookie("identity",verify.IMEI);
		                		window.location.href = '#index';
		                		contactList.loadContactList();
		                	}

		                }
		            };
		           
		            $.ajax(option);	    		
	    	});						
		}	
};


var message = { 
		
		captionMap: {},
		
		loadMessage: function() {	
			
		   var option = {
				   type : "POST",
				   dataType : "json",
				   data : {},
				   url : 'getMessageList.action',

				   success : function(ret) {
					   resp = ret;
					   message.initMessageList(resp.msgList);
				   }
		   };
		   $.ajax(option);   	            
	    },
	    	   	 
	    initMessageList: function(list) {
	    	message.captionMap = {};	    	
	    	$.each(list, function(index, value) {	    		
				var id = value.id;								
				var content = value.content;				
				var createDate = value.createDate.substring(0,10);				
				var date;				
				if ( (date = message.captionMap[createDate]) == null) {
					 date = $("<li id='"+createDate+"'data-role='list-divider'>"+createDate+"</li>");
					 message.captionMap[createDate] = date;
					 $("#"+createDate).remove();
					 $("#msglistView").append(date);
				} 
				$("#"+id).remove();
				date.after($("<li id='"+id+"'>"+content+"</a></li>"));
	    	}); 
	    	
	    	$("#msglistView").listview('refresh'); 
	    }
		
};

var language = {
		
		chooseLanguage:function(lang) {
		   $("#languageFormId").submit();		   
		}
		
};


function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

